package com.heima.xxljob.job;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class HelloJob {
   @XxlJob("demo")
    public void helloJob(){
        System.out.println("简单任务执行了。。。。");
    }

    @XxlJob("sharding")
    public void sharding(){
        List<Integer> list = getList();
        //当前任务的分片  0  1  2
        int shardIndex = XxlJobHelper.getShardIndex();
        //分片数量(总任务数)
        int shardTotal = XxlJobHelper.getShardTotal();
        for (Integer integer : list) {
            if (integer%shardTotal==shardIndex){
                System.out.println("当前第"+shardIndex+"分片执行了，任务项为："+integer);
            }
        }
    }
    public List<Integer> getList(){
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            list.add(i);
        }
        return list;
    }
}